var url = window.location.href; // 获取完整的URL  
var searchParams = new URLSearchParams(window.location.search); // 获取URL中的参数部分  
uid = searchParams.get("uid");
// 获取初始数据
$.ajax({
    url: '/noinfo/update_product_get/',
    type: 'GET',
    async: false,
    dataType: 'json',
    data: {
        'id': uid,
    },
    success: function(data) {
        product = JSON.parse(data.product)[0].fields;
        $("input[name='type']").each(function() {
            if ($(this).val() == product.type) {
              $(this).prop("checked", true);
            } else {
              $(this).prop("checked", false);
            }
          });
        $.each(product, function(key, value) {
            if (key != 'type' && key !='create_time' && key !='update_time' && key !='playmode' && key !='dim') {
                $('#' + key).val(value); 
            }
        });
        var type = $("input[name='type']:checked").val();
        updateInput(type);
        $('#playmode').val(product.playmode);
        $('#dim').val(product.dim);
        var playmode = product.playmode;
        updateMode(type, playmode);
    }
});

window.onload = function() {
    // 监听集数，更新末集
    $('#episodes').on('input', function() {
        $('#episode_end').val($('#episodes').val());
    });
    // 监听末集，更新集数
    $('#episode_end').on('input', function() {
        var episodes = $('#episode_end').val() - $('#episode_start').val() + 1;
        $('#episodes').val(episodes);
    });
    // 监听开播日期，更新完播日期
    $('#date_start').on('input', function() {
        var type = $("input[name='type']:checked").val();
        var date_start = $('#date_start').val();
        if(type == '动画' && date_start.length == 10) {
            var episodediff =  ($('#episode_end').val() - $('#episode_start').val()) * 7; 
            var date_s = new Date(date_start);
            var date_e = date_s.setDate(date_s.getDate() + episodediff);
            var date_end= new Date(date_e);
            var year = date_end.getFullYear();
            var month = ('0' + (date_end.getMonth() + 1)).slice(-2);
            var day = ('0' + date_end.getDate()).slice(-2);
            var date_end = year + '-' + month + '-' + day; 
            $('#date_end').val(date_end);
        }
    });
    // 监听形式选项，修改输入框条目
    $("#playmode").on("change", function() {
        var type = $("input[name='type']:checked").val();
        var playmode = $(this).val();
        updateMode(type, playmode);
    });
    
};

// 清除并批量添加option选项
function updateOption(selectId, texts) {
    $('#' + selectId).empty();
    $.each(texts, function(index, text) {
        var option = $('<option>').text(text);
        $('#' + selectId).append(option);
    });
    $('#' + selectId).val(null);
}

// 输入框标题更新
function updateTitle(texts) {
    $('#date_start-title').text(texts[0]);
    $('#date_end-title').text(texts[1]);
    $('#director-title').text(texts[2]);
}

// 依据类型修改输入框条目及格式
function updateInput(type) {    
    $(".form").find(":hidden").show();
    $('datalist').hide();
    if (type=='动画') {
        $('#chapters-div, #words-div').hide();
        var anime_playmode = ['TV', '电影', '剧场版', '番外篇', '特别篇', 'OVA', 'OAD','剧场OVA'];
        updateOption('playmode', anime_playmode);
        var anime_dim = ['二维', '三维', '三渲二'];
        updateOption('dim', anime_dim);
        var anime_title = ['开播时间', '完播时间', '导演'];
        updateTitle(anime_title);
    } else if (type=='电影') {
        $('#episodes-div, #episode_start-div, #episode_end-div, #date_end-div, #chapters-div, #words-div').hide();
        var movie_playmode = ['院线', '网络'];
        updateOption('playmode', movie_playmode);
        var movie_dim = ['真人', '二维', '三维', '三渲二'];
        updateOption('dim', movie_dim);
        var movie_title = ['上映时间', '完播时间', '导演'];
        updateTitle(movie_title);
    } else if (type=='电视剧') {
        $('#playmode-div, #dim-div, #chapters-div, #words-div').hide();
        var teleplay_title = ['开播时间', '完播时间', '导演'];
        updateTitle(teleplay_title);
    } else if (type=='小说') {
        $('#playmode-div, #dim-div, #episodes-div, #episode_start-div, #episode_end-div, #duration-div, #producer-div').hide();
        var novel_title = ['首发时间', '完结时间', '作者'];
        updateTitle(novel_title);
        
    }
};

// 依据形式修改输入框
function updateMode(type, playmode) {
    if(type == '动画') {
        if(playmode == '电影' || playmode == '剧场版') {
            $('#episodes-div, #episode_start-div, #episode_end-div, #date_end-div').hide();
        } else {
            $('#episodes-div, #episode_start-div, #episode_end-div, #date_end-div').show();
        }
    }
}

// 类型变化监听
$('input[name="type"]').on('change', function() {
    var type = $(this).val();
    updateInput(type);
    $('input').not('[name="type"]').val('');
});


// 提交数据
$('#submit').click(function() {
    var type = $("input[name='type']:checked").val();
    var theme = $("#theme").val();
    var title = $("#title").val();
    var season = $("#season").val();
    var subtitle = $("#subtitle").val();
    var playmode = $("#playmode").val();
    var dim = $("#dim").val();
    var episodes = $("#episodes").val();
    var duration = $("#duration").val();
    var episode_start= $("#episode_start").val();
    var episode_end = $("#episode_end").val();
    var chapters = $("#chapters").val();
    var words = $("#words").val();
    var date_start = $("#date_start").val();
    var date_end = $("#date_end").val();
    var producer = $("#producer").val();
    var director = $("#director").val();
    // 发送AJAX请求
    $.ajax({
        url: '/noinfo/update_product_post/',
        type: 'POST',
        dataType: 'json',
        data: { 
            id: uid,
            type: type,
            theme: theme,
            title: title,
            season: season,
            subtitle: subtitle,
            playmode: playmode,
            dim: dim,
            episodes: episodes,
            duration: duration,
            episode_start: episode_start,
            episode_end: episode_end,
            chapters: chapters,
            words: words,
            date_start: date_start,
            date_end: date_end,
            producer: producer,
            director: director,
        },
        success: function(data) {
            if(data.code_product == 'success') {
                var hint_product = '《' + title + '》作品更新完成';
            } else {
                var hint_product = data.code_product;
            }
            if(data.code_theme== 'create') {
                var hint_theme = '《' + theme + '》系列增加完成';
            } else  if(data.code_theme== 'update') {
                var hint_theme = '《' + theme + '》系列更新完成';
            } else if(data.code_theme== 'notupdate'){
                var hint_theme = '';
            } else {
                var hint_theme = data.code_theme;
            }
            hint = hint_product + '  ' + hint_theme;
            alert(hint);
            location.reload();
        },
        error: function(xhr, status, error) {
            alert('ajax执行失败');
          }
        });
})
